package com.xinqi.modules.sys.util;

import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @version V1.0
 * @Title:
 * @Description:
 * @author: dzy
 * @date: 2023/11/16 09:00
 * @Copyright 星齐科技（云南）有限公司 - Powered By 技术部
 */
public class IKSegmenterUtil {

    /**
     * 分词公共方法
     * 缺点有些词不分-比如高中语文
     */
    public static List<String> iKSegmenter(String text) {
        List<String> list = new ArrayList<>();
        try {
            StringReader re = new StringReader(text.trim());
            IKSegmenter ik= new IKSegmenter(re,true);
            Lexeme lex;
            while((lex = ik.next()) != null) {
                list.add(lex.getLexemeText());
            }
        } catch (Exception e) {
        }
        return list;
    }

    /**
     * 分字公共方法
     */
    public static List<String> singleSplice(String text) {
        List<String> list = new ArrayList<>();
        if (Objects.nonNull(text)) {
            char[] charArray = text.trim().toCharArray();
            for (char c : charArray) {
                String single = String.valueOf(c);
                list.add(single);
            }
        }
        return list;
    }

}
